import { defineStore } from 'pinia'
import { ref } from 'vue'
import { CaseApi } from '@/api/case'

export const useCaseStore = defineStore('case', () => {
    // 状态
    const cases = ref([])
    const currentCase = ref(null)
    const isLoading = ref(false)
    const error = ref(null)
    const pagination = ref({
        page: 1,
        pageSize: 10,
        total: 0
    })

    // 获取案件列表
    const fetchCases = async (params = {}) => {
        isLoading.value = true
        error.value = null
        try {
            const query = { ...pagination.value, ...params }
            const res = await CaseApi.getCaseList(query)
            cases.value = res.data
            pagination.value.total = res.pagination.total
            return res
        } catch (err) {
            error.value = err.message || '获取案件列表失败'
            throw err
        } finally {
            isLoading.value = false
        }
    }

    // 获取案件详情
    const fetchCaseDetail = async (id) => {
        isLoading.value = true
        error.value = null
        try {
            const res = await CaseApi.getCaseDetail(id)
            currentCase.value = res.data
            return res
        } catch (err) {
            error.value = err.message || '获取案件详情失败'
            throw err
        } finally {
            isLoading.value = false
        }
    }

    // 创建案件
    const createCase = async (data) => {
        isLoading.value = true
        error.value = null
        try {
            const res = await CaseApi.createCase(data)
            cases.value.unshift(res.data)
            return res
        } catch (err) {
            error.value = err.message || '创建案件失败'
            throw err
        } finally {
            isLoading.value = false
        }
    }

    // 更新案件
    const updateCase = async (id, data) => {
        isLoading.value = true
        error.value = null
        try {
            const res = await CaseApi.updateCase(id, data)
            // 更新当前案件
            if (currentCase.value?.id === id) {
                currentCase.value = { ...currentCase.value, ...data }
            }
            // 更新列表中的案件
            const index = cases.value.findIndex(item => item.id === id)
            if (index !== -1) {
                cases.value[index] = { ...cases.value[index], ...data }
            }
            return res
        } catch (err) {
            error.value = err.message || '更新案件失败'
            throw err
        } finally {
            isLoading.value = false
        }
    }

    // 删除案件
    const deleteCase = async (id) => {
        isLoading.value = true
        error.value = null
        try {
            await CaseApi.deleteCase(id)
            cases.value = cases.value.filter(item => item.id !== id)
            if (currentCase.value?.id === id) {
                currentCase.value = null
            }
            return true
        } catch (err) {
            error.value = err.message || '删除案件失败'
            throw err
        } finally {
            isLoading.value = false
        }
    }

    // 提交审批
    const submitCase = async (id, comment) => {
        isLoading.value = true
        error.value = null
        try {
            const res = await CaseApi.submitForReview(id, comment)
            // 更新案件状态
            if (currentCase.value?.id === id) {
                currentCase.value.status = 'reviewing'
            }
            const index = cases.value.findIndex(item => item.id === id)
            if (index !== -1) {
                cases.value[index].status = 'reviewing'
            }
            return res
        } catch (err) {
            error.value = err.message || '提交审批失败'
            throw err
        } finally {
            isLoading.value = false
        }
    }

    const setCase = (row)=>{
        currentCase.value = row
    }

    const getCase = ()=>{
        return currentCase.value
    }

    return {
        cases,
        currentCase,
        isLoading,
        error,
        pagination,
        setCase,
        getCase,
        fetchCases,
        fetchCaseDetail,
        createCase,
        updateCase,
        deleteCase,
        submitCase
    }
})